package net.wanho.service.impl;

import net.wanho.po.Dept;
import net.wanho.mapper.DeptMapper;
import net.wanho.service.DeptService;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author 汤小洋
 * @since 2023-08-18
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class DeptServiceImpl implements DeptService {

    private final DeptMapper deptMapper;

    /**
     * 部门表分页查询
     * @param pageNum
     * @param pageSize
     * @param dept
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo findPage(int pageNum, int pageSize, Dept dept) {
        if(ObjectUtil.isNull(dept)){
            dept = new Dept();
        }

        // 分页
        PageHelper.startPage(pageNum,pageSize);
        List<Dept> deptList = deptMapper.select(dept);
        return new PageInfo(deptList);
    }

    /**
     * 根据主键查询部门表
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Dept findById(Serializable id) {
        return deptMapper.selectById(id);
    }

    /**
     * 部门表新增
     * @param dept
     */
    @Override
    public void add(Dept dept) {
        deptMapper.insert(dept);
    }

    /**
     * 修改部门表
     * @param dept
     */
    @Override
    public void modify(Dept dept) {
        deptMapper.update(dept);
    }

    /**
     * 根据主键删除部门表
     * @param id
     */
    @Override
    public void removeById(Serializable id) {
        if(((String)id).contains(",")){
            deptMapper.deleteByIds(((String)id).split(","));
        }else {
            deptMapper.deleteById(id);
        }
    }
}
